<template>
  <div>
    <!-- 导航面包屑 -->
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品分类</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片区域 -->
    <el-card class="box-card">
      <el-row>
        <el-col>
          <el-button type="primary" @click="addCate">添加分类</el-button>
        </el-col>
      </el-row>
      <tree-table
        class="tree_table"
        :data="cateList"
        :columns="columns"
        show-index
        border
        index-text="#"
        :selection-type="false"
        :expand-type="false"
      >
        <!-- 是否有效 -->
        <template slot="isok" slot-scope="scope">
          <i
            class="el-icon-success"
            v-if="scope.row.cat_deleted === false"
            style="color: lightgreen"
          ></i>
          <i class="el-icon-error" v-else style="color: red"></i>
        </template>
        <!-- 排序 -->
        <template slot="order" slot-scope="scope">
          <el-tag v-if="scope.row.cat_level == 0">一级</el-tag>
          <el-tag type="success" v-else-if="scope.row.cat_level == 1"
            >二级</el-tag
          >
          <el-tag type="warning" v-else>三级</el-tag>
        </template>
        <!-- 操作 -->
        <template slot="handle" slot-scope="scope">
          <!-- 编辑按钮 -->
          <el-button
            type="primary"
            icon="el-icon-edit"
            size="small"
            @click="Edit(scope.row.cat_id)"
            >编辑</el-button
          >
          <!-- 删除按钮 -->
          <el-button
            type="danger"
            icon="el-icon-delete"
            size="small"
            @click="removeUserById(scope.row.cat_id)"
            >删除</el-button
          >
        </template>
      </tree-table>
      <!-- 页脚 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[3, 5, 10, 15]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>
    <el-dialog
      title="添加分类"
      :visible.sync="addCatedialogVisible"
      width="50%"
    >
      <el-form
        :model="addCateForm"
        :rules="addCaterules"
        ref="addCateruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="分类名称:" prop="cat_name">
          <el-input v-model="addCateForm.cat_name"></el-input>
        </el-form-item>
        <el-form-item label="父级分类:">
          <!-- props 绑定配置项  options 绑定数据数组 -->
          <el-cascader
            v-model="selectCateList"
            :options="addParentList"
            :props="cascaderProps"
            @change="addCatehandleChange"
            :change-on-select="true"
            clearable
          ></el-cascader>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addCatedialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleaddCate">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 添加 编辑角色 信息 -->
    <el-dialog
      title="编辑角色"
      :visible.sync="editdialogVisible"
      width="50%"
      @close="editFormClose"
    >
      <el-form
        :model="editForm"
        :rules="editrules"
        ref="editFormref"
        label-width="100px"
      >
        <el-form-item label="分类名称" prop="cat_name">
          <el-input v-model="editForm.cat_name"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editdialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="editUser">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      // 角色文件
      cateList: [],
      // 请求 参数
      queryInfo: {
        type: 3,
        pagenum: 1,
        pagesize: 5,
      },
      // 总条数
      total: 0,
      // 表格配置
      columns: [
        {
          label: '分类名称',
          prop: 'cat_name',
        },
        {
          label: '是否有效',
          prop: 'cat_deleted',
          // 表示为 自定义模板
          type: 'template',
          // 模板名称
          template: 'isok',
        },
        {
          label: '排序',
          prop: 'cat_level',
          // 表示为 自定义模板
          type: 'template',
          // 模板名称
          template: 'order',
        },
        {
          label: '操作',
          // 表示为 自定义模板
          type: 'template',
          // 模板名称
          template: 'handle',
        },
      ],
      // 添加 分类 弹框
      addCatedialogVisible: false,
      // 分类 对象
      addCateForm: {
        cat_name: '',
        // 父级分类id 默认为0
        cat_pid: 0,
        // 不能为空，`0`表示一级分类；`1`表示二级分类；`2`表示三级分类
        cat_level: 0,
      },
      addCaterules: {
        cat_name: [
          {
            required: true,
            message: '请输入分类名称',
            trigger: 'blur',
          },
        ],
      },
      // 编辑用户初始状态
      editdialogVisible: false,
      // 编辑
      editForm: {
        cat_name: '',
        cat_deleted: '',
      },
      editrules: {
        cat_name: [{ required: true, message: '请角色名称', trigger: 'blur' }],
      },
      // 存储 父级分类
      addParentList: [],
      // id 数组
      selectCateList: [],
      cascaderProps: {
        expandTrigger: 'hover',
        value: 'cat_id',
        label: 'cat_name',
        children: 'children',
      },
    }
  },
  methods: {
    async getCateList() {
      const { data } = await this.$http.get('categories', {
        params: this.queryInfo,
      })
      // console.log(data)
      this.cateList = data.data.result
      this.total = data.data.total
    },
    handleSizeChange(newSize) {
      this.queryInfo.pagesize = newSize
      this.getCateList()
    },
    handleCurrentChange(newCurrent) {
      this.queryInfo.pagenum = newCurrent
      this.getCateList()
    },
    // 添加分类 事件
    addCate() {
      this.getParentCate()
      this.addCatedialogVisible = true
    },
    // 获取父级分类 方法
    async getParentCate() {
      const { data } = await this.$http.get('categories', {
        params: { type: 2 },
      })
      if (data.meta.status !== 200) {
        return this.$message.error('获取父级分类失败')
      }
      this.addParentList = data.data
    },
    // 改变 选择对象
    addCatehandleChange() {
      console.log('2121', this.selectCateList)
      // 如果选择的数组长度为0则没有选中任何一项，否则就是选中了某一项
      if (this.selectCateList.length > 0) {
        // 父级分类的 id
        this.addCateForm.cat_pid = this.selectCateList[
          this.selectCateList.length - 1
        ]
        // 当前分类的等级赋值
        this.addCateForm.cat_level = this.selectCateList.length
      } else {
        this.addCateForm.cat_pid = 0
        this.addCateForm.cat_level = 0
      }
    },
    async handleaddCate() {
      console.log(this.addCateForm)
      const { data } = await this.$http.post('categories', this.addCateForm)
      console.log('ss', data)
      this.getParentCate()
      this.getCateList()
      this.addCatedialogVisible = false
    },
    async Edit(id) {
      const { data } = await this.$http.get('categories/' + id)
      console.log('dd', data)
      if (data.meta.status !== 200) {
        this.$message.error('获取商品分类数据失败')
      }
      this.editForm = data.data
      this.editdialogVisible = true
    },
    // 点击编辑确定按钮
    async editUser() {
      const { data } = await this.$http.put(
        'categories/' + this.editForm.cat_id,
        { cat_name: this.editForm.cat_name }
      )
      console.log('cc', data)
      this.getParentCate()
      this.getCateList()
      this.editdialogVisible = false
    },
    // 监听 编辑菜单 关闭事件
    editFormClose() {
      // 重置状态
      this.$refs.editFormref.resetFields()
    },
    // 点击删除
    removeUserById(id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(async () => {
          const { data } = await this.$http.delete('categories/' + id)
          if (data.meta.status !== 200) return this.$message.error('删除失败')
          this.getCateList()
          this.$message({
            type: 'success',
            message: '删除成功!',
          })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除',
          })
        })
    },
  },
  created() {
    this.getCateList()
  },
}
</script>

<style lang='less' scoped>
.tree_table {
  margin-top: 15px;
}
.el-cascader-menu {
  height: 300px;
}
</style>
